package com.delivery.www.servlet;

import com.delivery.www.dao.DaoImpl.StoreDaoImpl;
import com.delivery.www.po.Coupon;
import com.delivery.www.po.Dish;
import com.delivery.www.po.Store;
import com.delivery.www.po.User;
import com.delivery.www.service.ServiceImpl.CouponServiceImpl;
import com.delivery.www.service.ServiceImpl.DishServiceImpl;
import com.delivery.www.service.ServiceImpl.OrderServiceImpl;
import com.delivery.www.service.ServiceImpl.UserServiceImpl;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import static com.delivery.www.servlet.UserCommentPage.commentPage;
import static com.delivery.www.servlet.UserPage.userPage;

public class UserStorePage {
    public static void storePage(User user) {
        Scanner scanner = new Scanner(System.in);
        StoreDaoImpl storeDao = new StoreDaoImpl();
        OrderServiceImpl orderService = new OrderServiceImpl();
        DishServiceImpl dishService = new DishServiceImpl();
        UserServiceImpl userService = new UserServiceImpl();
        CouponServiceImpl couponService = new CouponServiceImpl();
        List<Store> stores = storeDao.getAllStore();
        for (Store store : stores) {
            System.out.println("==================");
            System.out.println("店铺id:" + store.getStoreId());
            System.out.println("店铺名:" + store.getStoreName());
            System.out.println("店铺简介:" + store.getIntroduce());
            System.out.println("菜品数:" + store.getDishNum());
            System.out.println("评论数:" + store.getCommentNum());
            System.out.println("===================");
        }
        System.out.println("请输入你要进入的店铺id(返回请按0);):");
        int i = scanner.nextInt();
        List<Coupon> coupons = couponService.getCoupon(i);
        if (i == 0) {
            userPage(user);
        } else {
            System.out.println("店铺有以下优惠券");
            for (Coupon coupon : coupons) {
                System.out.println("========================");
                System.out.println("优惠券id：" + coupon.getCouponId() + " 优惠券满" + coupon.getMin() + "减" + coupon.getValue());
                System.out.println("========================");
            }
            System.out.println("付款时可选择使用");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("请输入你要进行的操作(返回请按0)：");
        System.out.println("1.查看店铺评论");
        System.out.println("2.下单");
        int i6 = scanner.nextInt();
        while (i6 != 1 && i6 != 2 && i6 != 0) {
            System.out.println("输入错误！请重新输入");
            i6 = scanner.nextInt();
        }
        if (i6 == 0) {
            storePage(user);
        }
        if (i6 == 1) {
            commentPage(i, user);
        }
        if (i6 == 2) {
            List<Dish> dishes = dishService.getDish(i);
            for (Dish dish : dishes) {
                System.out.println("===========================");
                System.out.println("菜品id:" + dish.getDishId());
                System.out.println("菜品名:" + dish.getDishName());
                System.out.println("菜品价格:" + dish.getPrice());
                System.out.println("菜品描述:" + dish.getDescribe());
                System.out.println("===========================");
            }
            System.out.println("请输入你所要下单的菜品id(返回请按0):");
            int i1 = scanner.nextInt();
            if (i1 == 0) {
                storePage(user);
            } else {
                Dish adish = dishService.getOneDish(i1);
                System.out.println("是否要使用抵用券:");
                System.out.println("1.是");
                System.out.println("2.否");
                int i3 = scanner.nextInt();
                while (i3 != 1 && i3 != 2) {
                    System.out.println("输入错误！请重新输入");
                    i3 = scanner.nextInt();
                }
                if (i3 == 1) {
                    System.out.println("请输入抵用券id：");
                    int i4 = scanner.nextInt();
                    Coupon coupon = couponService.getOneCoupon(i4);
                    if (adish.getPrice() > coupon.getMin()) {
                        if (user.getBalance() < adish.getPrice() - coupon.getValue()) {
                            System.out.println("余额不足，请先充值");
                            System.out.println("是否前往充值(返回请按0)");
                            System.out.println("1.是");
                            System.out.println("2.否");
                            int i2 = scanner.nextInt();
                            while (i2 != 0 && i2 != 1 && i2 != 2) {
                                System.out.println("输入错误！请重新输入");
                                i2 = scanner.nextInt();
                            }
                            if (i2 == 0) {
                                storePage(user);
                            }
                            if (i2 == 1) {
                                System.out.println("请输入你所要充值的金额");
                                Float v = scanner.nextFloat();
                                boolean recharge = userService.recharge(user.getUserId(), v);
                                if (recharge) {
                                    System.out.println("充值成功！即将返回");
                                    user = userService.login(user.getUserName(), user.getPassword());
                                    try {
                                        TimeUnit.SECONDS.sleep(1);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    storePage(user);
                                }
                            }
                        } else {
                            boolean b = orderService.addOrder(user.getUserId(), i, i1, adish.getPrice() - coupon.getValue());
                            if (b) {
                                System.out.println("下单成功！即将跳转");
                                try {
                                    TimeUnit.SECONDS.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                storePage(user);
                            }
                        }
                    } else {
                        System.out.println("不满足使用优惠券的最低金额！");
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("是否继续下单");
                        System.out.println("1.是");
                        System.out.println("2.否");
                        int i5 = scanner.nextInt();
                        while (i5 != 1 && i5 != 2) {
                            System.out.println("输入错误！请重新输入");
                            i5 = scanner.nextInt();
                        }
                        if (i5 == 1) {
                            if (user.getBalance() < adish.getPrice()) {
                                System.out.println("余额不足，请先充值");
                                System.out.println("是否前往充值(返回请按0)");
                                System.out.println("1.是");
                                System.out.println("2.否");
                                int i2 = scanner.nextInt();
                                while (i2 != 1 && i2 != 2 && i2 != 0) {
                                    System.out.println("输入错误！请重新输入");
                                    i2 = scanner.nextInt();
                                }
                                if (i2 == 0) {
                                    storePage(user);
                                }
                                if (i2 == 1) {
                                    System.out.println("请输入你所要充值的金额");
                                    Float v = scanner.nextFloat();
                                    boolean recharge = userService.recharge(user.getUserId(), v);
                                    if (recharge) {
                                        System.out.println("充值成功！即将返回");
                                        try {
                                            TimeUnit.SECONDS.sleep(1);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        storePage(user);
                                    }
                                }
                                if (i2 == 2) {
                                    storePage(user);
                                }
                            } else {
                                boolean b = orderService.addOrder(user.getUserId(), i, i1, adish.getPrice());
                                if (b) {
                                    System.out.println("下单成功！即将跳转");
                                    try {
                                        TimeUnit.SECONDS.sleep(1);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    storePage(user);
                                }
                            }
                        } else {
                            storePage(user);
                        }
                    }
                } else {
                    if (user.getBalance() < adish.getPrice()) {
                        System.out.println("余额不足，请先充值");
                        System.out.println("是否前往充值(返回请按0)");
                        System.out.println("1.是");
                        System.out.println("2.否");
                        int i2 = scanner.nextInt();
                        while (i2 != 1 && i2 != 2 && i2 != 0) {
                            System.out.println("输入错误！请重新输入");
                            i2 = scanner.nextInt();
                        }
                        if (i2 == 0) {
                            storePage(user);
                        }
                        if (i2 == 1) {
                            System.out.println("请输入你所要充值的金额");
                            Float v = scanner.nextFloat();
                            boolean recharge = userService.recharge(user.getUserId(), v);
                            if (recharge) {
                                System.out.println("充值成功！即将返回");
                                try {
                                    TimeUnit.SECONDS.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                storePage(user);
                            }
                        }
                    } else {
                        boolean b = orderService.addOrder(user.getUserId(), i, i1, adish.getPrice());
                        if (b) {
                            System.out.println("下单成功！即将跳转");
                            try {
                                TimeUnit.SECONDS.sleep(1);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            storePage(user);
                        }
                    }
                }
            }
        }
    }
}
